<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="流程KEY" prop="modelKey">
        <el-input
          v-model="queryParams.processDefinitionKey"
          placeholder="请输入流程KEY"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程名称" prop="modelName">
        <el-input
          v-model="queryParams.processDefinitionName"
          placeholder="请输入流程名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <div>
          <el-checkbox v-model="queryParams.latestVersion">最新版本</el-checkbox>
          <el-checkbox v-model="queryParams.suspended">挂起的</el-checkbox>
          <el-checkbox v-model="queryParams.active">激活的</el-checkbox>
        </div>
      </el-form-item>
       <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
   
    <el-table v-loading="loading" :data="processList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流程定义ID" prop="id" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="流程定义KEY" prop="key" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="流程定义名称" prop="name" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="所属分类" prop="category" :show-overflow-tooltip="true" width="150" >
         <template slot-scope="scope">
              <el-tag prop="category" v-for="(item, index) in modelCateOptions"   v-if="scope.row.category==item.dictValue">{{item.dictLabel}}</el-tag>
         </template>
      </el-table-column>
      <el-table-column label="版本号" prop="version" align="center">
        <template slot-scope="scope"><el-tag>V{{ scope.row.version }}</el-tag></template>
      </el-table-column>
      <el-table-column label="状态" prop="suspended" align="center">
          <template slot-scope="scope"><span>{{ scope.row.suspended?'挂起':'激活' }}</span></template>
      </el-table-column>
      <el-table-column label="表单KEY(Start)" prop="formKey" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="操作" align="center">
         <template slot-scope="{row}">
          <el-dropdown>
              <span class="el-dropdown-link">操作<i class="el-icon-arrow-down el-icon--right"></i></span>
               <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item
                        icon="el-icon-edit" 
                        v-hasPermi="['flowable:process:query']"
                        @click.native="btnView(row)">查看
                      </el-dropdown-item>
                      <el-dropdown-item icon="el-icon-view" 
                    
                      divided @click.native="btnImage(row.id)">流程图
                      </el-dropdown-item>
                      <el-dropdown-item
                      v-hasPermi="['flowable:process:auth']"
                      icon="el-icon-edit" divided
                      @click.native="btnProcessDefinitionIdentityLink(row)">流程授权
                      </el-dropdown-item>
                      <el-dropdown-item icon="el-icon-edit"
                       v-hasPermi="['flowable:process:xml']"
                       divided @click.native="btnExport(row)">导出
                      </el-dropdown-item>
                      <el-dropdown-item icon="el-icon-edit"
                       v-hasPermi="['flowable:process:opt']"
                       divided @click.native="btnSuspendOrActivate(row.id,row.suspended)">
                          {{row.suspended?'激活':'挂起' }}
                      </el-dropdown-item>
                      <el-dropdown-item icon="el-icon-delete"
                       v-hasPermi="['flowable:process:remove']"
                       divided @click.native="btnDelete(row.id,false)">删除
                      </el-dropdown-item>
                      <el-dropdown-item icon="el-icon-delete"
                       v-hasPermi="['flowable:process:remove']"
                       divided @click.native="btnDelete(row.id,true)">删除包含实例
                      </el-dropdown-item>
                       
               </el-dropdown-menu>
          </el-dropdown>
         </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
     <process-image v-if="dialogProcessImageVisible" :visible.sync="dialogProcessImageVisible"
                       :processDefinitionId="selectedProcessDefinitionId"></process-image>
                       
      <el-dialog title="流程模型" :visible.sync="dialogFormVisible">
          <el-form ref="dataForm" :model="form"
                    label-position="right" label-width="110px">
              <el-form-item label="模型key" prop="key">
                  <el-input v-model="form.key" readonly="readonly"/>
              </el-form-item>
              <el-form-item label="模型名称" prop="name">
                  <el-input v-model="form.name" readonly="readonly"/>
              </el-form-item>
              <el-form-item label="模型类别" prop="category">
                 <el-select v-model="form.category" placeholder="模型类别" disabled>
                    <el-option
                      v-for="dict in modelCateOptions"
                      :key="dict.dictValue"
                      :label="dict.dictLabel"
                      :value="dict.dictValue"
                    />
                </el-select>
              </el-form-item>
              <el-form-item label="模型描述" prop="description" readonly="readonly">
                  <el-input v-model="form.description"/>
              </el-form-item>
          </el-form>
        
      </el-dialog>
       <process-definition-identity-link v-if="dialogProcessDefinitionIdentityLinkVisible"
                                          :visible.sync="dialogProcessDefinitionIdentityLinkVisible"
                                          :processDefinitionId="selectedProcessDefinitionId"
                                          :processDefinitionName="selectedProcessDefinitionName"></process-definition-identity-link>
  </div>
</template>
<script>
import { listProcess,downloadXml,delProcess,optProcess } from "@/api/flowable/process";
import ProcessImage from "../components/ProcessImage";
import ProcessDefinitionIdentityLink from "./processDefinitionIdentityLink";

export default {
  name: "process",
  components: {ProcessImage,ProcessDefinitionIdentityLink},
  data() {
    return {
      selectedProcessDefinitionId:undefined,
      selectedProcessDefinitionName:undefined,
      dialogProcessDefinitionIdentityLinkVisible:false,
      modelCateOptions:[],
      form:{},
      dialogFormVisible:false,
      dialogProcessImageVisible:false,
      selectedRecords: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      processList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processDefinitionKey: undefined,
        latestVersion:false,
        suspended:false,
        active:false
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
     this.getDicts("model_cate").then((response) => {
        this.modelCateOptions = response.data;
      });
  },
  methods: {
    getList() {
      this.loading = true;
      listProcess(this.queryParams).then(
        response => {
          this.processList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
     /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roleId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    btnImage(processDefinitionId) {
        this.selectedProcessDefinitionId = processDefinitionId
        this.dialogProcessImageVisible = true
    },
    btnExport(row) {
        //window.location.href=`${process.env.VUE_APP_BASE_API}` +'/flowable/process/xml?processDefinitionId='+row.id;
        downloadXml('/flowable/process/xml', 'get', {processDefinitionId: row.id}, row.name + '-v' + row.version + '.bpmn20.xml')
      
    },
    btnSuspendOrActivate(processDefinitionId, suspend) {
      let suspendOrActivate = suspend ? 'activate' : 'suspend'
      optProcess('/flowable/process/' + suspendOrActivate, {processDefinitionId}).then(({msg}) => {
          this.msgSuccess(msg);
          this.getList();
      })
    },
    btnDelete(id, cascade) {
        let ids = id ? [id] : this.selectedRecords.map(record => {
            return record.id
        })
        if (ids.length == 0) {
            this.msgError("请选择要删除的记录");
            return
        }
        if (ids.length > 1) {
            this.msgError('只能选择一条记录');
            return
        }
        delProcess(ids.toString(), cascade).then(({msg}) => {
            this.msgSuccess(msg);
            this.getList()
        })
    },
    btnView(row) {
        this.form = Object.assign({}, row)
        this.dialogFormVisible = true
        this.$nextTick(() => {
            this.$refs['dataForm'].clearValidate()
        })
    },
    btnProcessDefinitionIdentityLink(row) {
        this.selectedProcessDefinitionId = row.id
        this.selectedProcessDefinitionName = row.name
        this.dialogProcessDefinitionIdentityLinkVisible = true;
    },
     
  }
};
</script>